package com.qq.oauth;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import com.qq.util.ParseString;
import com.qq.util.QQRequest;
import com.qq.util.QParameter;
import com.qq.util.OauthKey;;

public class Token {

	 /**
	 * @brief get a request token by appid and appkey
	 *        rfc1738 urlencode
	 * @param $appid
	 * @param $appkey
	 *
	 * @return a string, the format as follow: 
	 *      oauth_token=xxx&oauth_token_secret=xxx
	 */
	public String getRequestToken(String appid, String appkey) {
		String url = "http://openapi.qzone.qq.com/oauth/qzoneoauth_request_token";
		List<QParameter> parameters = new ArrayList<QParameter>();
		OauthKey oauthKey = new OauthKey();
		oauthKey.customKey = appid;
		oauthKey.customSecrect = appkey;
		//The OAuth Call back URL(You should encode this url if it
		//contains some unreserved characters).
		QQRequest request = new QQRequest();
		String res = null;
		try {
			res = request.syncRequest(url, "GET", oauthKey, parameters, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return res;
	}
	
	/**
	 * @brief get a access token 
	 *        rfc1738 urlencode
	 * @param $appid
	 * @param $appkey
	 * @param $request_token
	 * @param $request_token_secret
	 * @param $vericode
	 *
	 * @return a string, as follows:
	 *      oauth_token=xxx&oauth_token_secret=xxx&openid=xxx&oauth_signature=xxx&oauth_vericode=xxx&timestamp=xxx
	 */
	public String getAccessToken(String appid, String appkey, String oauth_token, String oauth_token_secret, String oauth_vericode) {

		String url = "http://openapi.qzone.qq.com/oauth/qzoneoauth_access_token";
		List<QParameter> parameters = new ArrayList<QParameter>();
		OauthKey oauthKey = new OauthKey();
		oauthKey.customKey = appid;
		oauthKey.customSecrect = appkey;
		oauthKey.tokenKey = oauth_token;
		oauthKey.tokenSecrect = oauth_token_secret;
		QParameter oauth_vericodep = new QParameter("oauth_vericode",oauth_vericode);
		parameters.add(oauth_vericodep);

		QQRequest request = new QQRequest();
		String res = null;
		try {
			res = request.syncRequest(url, "GET", oauthKey, parameters, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return res;
	}
	
	/**
	 * @brief get a access token 
	 *        rfc1738 urlencode
	 * @param $appid
	 * @param $appkey
	 * @param $request_token
	 * @param $request_token_secret
	 * @param $openid
	 * @param $format
	 * 
	 * @return a xml or json as follows:
	 *	<?xml version="1.0" encoding="UTF-8"?> 
	 *	<data>
	 *	<ret>0</ret>
	 *	<msg></msg>
	 *	<nickname>Lamb</nickname> 
	 *	<figureurl>http://qzapp.qlogo.cn/qzapp/205076/F08332A45A14FEC1CFFA452E4E6FCACE/30</figureurl>
	 *	<figureurl_1>http://qzapp.qlogo.cn/qzapp/205076/F08332A45A14FEC1CFFA452E4E6FCACE/50</figureurl_1>
	 *	<figureurl_2>http://qzapp.qlogo.cn/qzapp/205076/F08332A45A14FEC1CFFA452E4E6FCACE/100</figureurl_2>
	 *	</data>
	 *	Content-type: text/html; charset=utf-8
	 *	{
	 *	"ret":0,
	 *	"msg":"",
	 *	"nickname":"Peter",
	 *	"figureurl":"http://exmple.qq.com/qzapp/000000000000000000000000000F4262/50",
	 *	} 
	 *  Content-type: text/html; charset=utf-8
	 * 	{
	 * 	"ret":2021,
	 * 	"msg":"请先登录"
	 * 	}    
	 *     
	 *     
	 *     
	 */
	public String getUserInfo(String appid, String appkey, String oauth_token, String oauth_token_secret, String openid, String format) {

		String url = "http://openapi.qzone.qq.com/user/get_user_info";
		List<QParameter> parameters = new ArrayList<QParameter>();
		OauthKey oauthKey = new OauthKey();
		oauthKey.customKey = appid;
		oauthKey.customSecrect = appkey;
		oauthKey.tokenKey = oauth_token;
		oauthKey.tokenSecrect = oauth_token_secret;
		
		QParameter openidp = new QParameter("openid",openid);
		parameters.add(openidp);
		QParameter formatp = new QParameter("format",format);
		parameters.add(formatp);
		
		QQRequest request = new QQRequest();
		String res = null;
		try {
			res = request.syncRequest(url, "GET", oauthKey, parameters, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return res;
	}
}
